from django.contrib import admin
from .models import Address

@admin.register(Address)
class AddressAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'name', 'phone', 'province', 'city', 'district', 'is_default', 'created_at']
    list_filter = ['is_default', 'province', 'city', 'created_at']
    search_fields = ['name', 'phone', 'detail', 'user__username']
    list_per_page = 20
    readonly_fields = ['created_at', 'updated_at']
    ordering = ['-created_at']
    
    fieldsets = [
        ('用户信息', {
            'fields': ['user', 'name', 'phone']
        }),
        ('地址信息', {
            'fields': ['province', 'city', 'district', 'detail']
        }),
        ('状态信息', {
            'fields': ['is_default']
        }),
        ('时间信息', {
            'fields': ['created_at', 'updated_at'],
            'classes': ['collapse']
        })
    ]

    def get_queryset(self, request):
        """优化查询，减少数据库查询次数"""
        return super().get_queryset(request).select_related('user') 